package com.moshang.blog.controller.web;

import com.moshang.blog.entity.TbMessage;
import com.moshang.blog.service.TbMessageService;
import com.moshang.blog.util.RestResponse;
import com.moshang.blog.core.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @program: blog
 * @description: 评论/留言控制器类
 * @author: moshang
 * @create: 2018-12-21 17:34
 **/
@Controller
@RequestMapping("message")
public class TbMessageController {
    @Autowired
    private TbMessageService tbMessageService;

    /**
     * 添加留言或者评论
     */
    @ApiOperation(value = "/add", httpMethod = "POST", notes = "添加评论/留言")
    @PostMapping("add")
    @ResponseBody
    public RestResponse add(TbMessage tbMessage, HttpServletRequest request){
        try {
            if(StringUtils.isBlank(tbMessage.getContent())){
                return RestResponse.failure("内容不能为空");
            }
          TbMessage tbMessage1=   tbMessageService.saveOrUpdateMessage(tbMessage,"INSERT",request);

            return RestResponse.success().setData(tbMessage1);
        }catch (AuthorizationException a){
            throw  a;
        }

    }
}
